package org.iipg.service.core.init;

import org.iipg.service.core.component.JSONLoader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: zhuhy163@163.com Date: 2020/9/30
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DataLoader implements ApplicationRunner {

	private final JSONLoader jsonLoader;

	@Value("${ph.data.base-path}")
	private String basePath;

	/**
	 * general json data load
	 */
	private void loadJsonData() {
		log.info("Start Load json data...");
		Map<String, Object> props = new HashMap<>();

		//myType
		props.put("type", "myType");
		jsonLoader.load(basePath + File.separator + "myType.json", "DomainInfo", props);

		log.info("Load json data done.");
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {

		try {
			log.info("Base Data Path: {}", basePath);

			// json data
			loadJsonData();
		}
		catch (Exception e) {
			log.warn(e.getMessage());
		}
	}

}
